package com.zl.petstore.ui;

import com.zl.petstore.dao.AccountDao;
import com.zl.petstore.domain.Account;
import com.zl.petstore.dao.mysql.AccountDaoImp;

import javax.swing.*;
import java.awt.*;

public class LoginFrame extends MyFrame {

    private AccountDao dao;
    private JTextField txtAccountid;
    private JPasswordField txtPassword;

    public LoginFrame() {
        super("用户登录", 400, 230);
        // 设置布局管理器为null
        getContentPane().setLayout(null);

        JLabel lblNewLabel1 = new JLabel("账号：");
        lblNewLabel1.setHorizontalAlignment(SwingConstants.RIGHT);
        lblNewLabel1.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        lblNewLabel1.setBounds(64, 32, 54, 15);
        getContentPane().add(lblNewLabel1);

        JLabel lblNewLabel2 = new JLabel("密码：");
        lblNewLabel2.setHorizontalAlignment(SwingConstants.RIGHT);
        lblNewLabel2.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        lblNewLabel2.setBounds(64, 75, 54, 15);
        getContentPane().add(lblNewLabel2);

        txtAccountid = new JTextField();
        txtAccountid.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        txtAccountid.setBounds(145, 29, 156, 21);
        getContentPane().add(txtAccountid);
        txtAccountid.setColumns(10);

        txtPassword = new JPasswordField();
        txtPassword.setColumns(10);
        txtPassword.setBounds(145, 72, 156, 21);
        getContentPane().add(txtPassword);

        JButton btnOK = new JButton("确定");
        //登录处理
        btnOK.addActionListener(e->{
            //TODO DAO
            dao = new AccountDaoImp();
            Account account = dao.findById(txtAccountid.getText());
            String passText = new String(txtPassword.getPassword());//char[]
            if(account != null && passText.equals(account.getPassword())) {
                //登录成功
                System.out.println("登录成功。");
                //界面跳转到商品列表窗口

                MainApp.account = account;

            }else {
                //登录失败
                System.out.println("登录失败。");
                JLabel label = new JLabel("您输入的账号或密码有误，请重新输入");
                JOptionPane.showMessageDialog(null, label,"登陆失败", JOptionPane.ERROR_MESSAGE);
            }
        });
        btnOK.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        btnOK.setBounds(49, 119, 93, 39);
        getContentPane().add(btnOK);

        JButton btnCancle = new JButton("取消");
        //取消处理
        btnCancle.addActionListener(e->{
            //退出系统
            System.exit(0);;
        });
        btnCancle.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        btnCancle.setBounds(236, 119, 93, 39);
        getContentPane().add(btnCancle);

    }

}
